﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace MiniFox.Data
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class Parameter
    {
        /// <summary>
        /// 
        /// </summary>
        public Parameter()
        {
            _val = DBNull.Value;
        }
        string _name;
        object _val;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        public Parameter(string name)
        {
            _name = name;
        }
        /// <summary>
        /// 
        /// </summary>
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string ParameterName
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        public Type ClrType
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        public ParameterDirection Direction
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        public byte Precision
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        public byte Scale
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        public int Size
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        public bool Nullable
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        public object DefaultValue
        {
            get 
            { 
                return _val;
            }
            set
            {
                object val = value;

                if (val == null)
                {
                    if (this.Nullable)
                        val = DBNull.Value;
                    else if (this.ClrType == typeof(string))
                        val = string.Empty;
                    else
                        throw new InvalidOperationException(string.Format(Resources.CommonMessages.MSG_CommonRequired, this.Name));
                }
                else if (val.GetType() != this.ClrType)
                {
                    try
                    {
                        val = Convert.ChangeType(val, this.ClrType);
                    }
                    catch
                    {
                        if (this.Nullable)
                            val = DBNull.Value;
                        else
                            throw new InvalidCastException(string.Format(Resources.CommonMessages.MSG_CommonTypeUnmatched, this.Name, this.ClrType.Name));
                    }
                }
                _val = val;
            }
        }
    }
}
